﻿namespace Sidvall.Net.Mail
{
    public class AttachmentCollection : System.Collections.ObjectModel.Collection<Attachment>
    {
        #region Public Members

        #region AddAttachmentFromFile

        public void AddAttachmentFromFile(string fileName)
        {
            AddAttachmentFromFile(fileName, null);
        }
        public void AddAttachmentFromFile(string fileName, string name)
        {
            var item = new Attachment()
            {
                AttachmentType = AttachmentType.File,
                Name = name,
                FileName = fileName,
            };
            Add(item);
        }

        #endregion
        #region AddAttachmentFromStream

        public void AddAttachmentFromStream(System.IO.Stream contentStream, string name)
        {
            var item = new Attachment()
            {
                AttachmentType = AttachmentType.Stream,
                Name = name,
                ContentStream = contentStream,
            };
            Add(item);
        }

        #endregion
        #region AddAttachmentFromString

        public void AddAttachmentFromString(string content, string name)
        {
            var item = new Attachment()
            {
                AttachmentType = AttachmentType.String,
                Name = name,
                Content = content,
            };
            Add(item);
        }

        #endregion
        #region AddRange

        public void AddRange(System.Collections.Generic.IEnumerable<Attachment> items)
        {
            if (items == null)
                return;
            foreach (var item in items)
                Add(item);
        }

        #endregion
        #region GetNameResolved

        public string GetNameResolved(Attachment item)
        {
            if (item == null)
                return null;
            if (!string.IsNullOrWhiteSpace(item.Name))
                return item.Name;
            if (!string.IsNullOrWhiteSpace(item.FileName))
                return System.IO.Path.GetFileName(item.FileName);
            var index = IndexOf(item);
            if (index < 0)
                return null;
            return "Attachment_" + (index + 1).ToString(System.Globalization.CultureInfo.InvariantCulture);
        }

        #endregion

        #endregion
    }
}
